﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using USERS.CYT369.COM.Models;

namespace USERS.CYT369.COM.Controllers
{
    public partial class NewsController
    {

        #region Articles
        [HttpPost]
        public IActionResult SaveArticle([FromBody] USERS.CYT369.COM.MODELS.Articles.Document document)
        {
            var retVal = new USERS.CYT369.COM.Models.ExecuteResult();
            document.EditorUserID = this.GetUserId();
            document.EditorName = this.UserSession.NickName;
            document.EditorFullName = this.UserSession.FullName;
            document.AuditStatusID = 0;
            if (string.IsNullOrEmpty(document.Title))
            {
                document.Title = "[未命名]";
            }

            if (USERS.CYT369.COM.DAL.DocumentService.Instance.SaveDocument(document))
            {
                retVal.Result = true;
                retVal.Data = document;
                retVal.Msg = "success";
            }

            return DefaultJson(retVal);
        }

        [HttpPost]
        public IActionResult GetDocumentByID(long id)
        {
            var document = USERS.CYT369.COM.DAL.DocumentService.Instance.GetDocumentByID(id, this.GetUserId());
            return DefaultJson(document);
        }

        [HttpPost]
        public IActionResult SubmitAudit(long id)
        {
            var retVal = new USERS.CYT369.COM.Models.ExecuteResult();
            retVal.Result = USERS.CYT369.COM.DAL.DocumentService.Instance.SubmitAudit(id, this.GetUserId());

            return DefaultJson(retVal);
        }

        [HttpDelete]
        public IActionResult DeleteArticle(string id)
        {
            var retVal = new USERS.CYT369.COM.Models.ExecuteResult();

            if (!string.IsNullOrEmpty(id))
            {
                var argsJson = Base64.Base64ToJSON(id);
                long? docId = argsJson.GetInteger("NID");
                if (docId > 0)
                {
                    retVal.Result = USERS.CYT369.COM.DAL.DocumentService.Instance.DeleteArticle(docId.Value, this.GetUserId());
                }
            }
            return DefaultJson(retVal);
        }

        /// <summary>
        /// 删除图片
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult RemoveArticleAttachment(long docId, long fileId)
        {
            var retVal = new USERS.CYT369.COM.Models.ExecuteResult();
            
            retVal.Result = USERS.CYT369.COM.DAL.DocumentService.Instance.DeleteArticleAttachment(docId, fileId, this.GetUserId());

            return DefaultJson(retVal);
        }

        #endregion

        #region MyRegion
        /// <summary>
        /// 栏目树
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JsonResult GetUserDocCategorys()
        {
            var not_id = Request.Query.GetInteger("NOT_ID");

            var categorys = new USERS.CYT369.COM.MODELS.Articles.UserDocCategory() {
                Id = 0,
                ParentID = 0,
                Name = "所有类型",
            };

            foreach (var item in USERS.CYT369.COM.DAL.DocumentService.Instance.GetUserDocCategorys(this.GetUserId(), 0, not_id)) {
                categorys.SubTypes.Add(item);
            }

            return DefaultJson(new USERS.CYT369.COM.MODELS.Articles.UserDocCategory[] { categorys });
        }

        #endregion
        //#region Document Types
        //public IActionResult GetDocumentTypes(int id)
        //{
        //    var documentTypes = USERS.CYT369.COM.DAL.DocumentService.Instance.GetDocumentTypes(id);

        //    return DefaultJson(documentTypes);
        //}

        //public IActionResult GetDocumentTypesByID(int id)
        //{
        //    var documentType = USERS.CYT369.COM.DAL.DocumentService.Instance.GetDocumentTypeByID(id);

        //    return DefaultJson(documentType);
        //}
        //#endregion




    }
}
